package com.jinzhi.jzweb.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzapi.common.controller.ApittController;
import com.jinzhi.jzweb.domain.ExaminationAnswerSheetComDO;
import com.jinzhi.jzweb.domain.ExaminationResultsComDO;
import com.jinzhi.jzweb.domain.StuSignUpComDO;
import com.jinzhi.jzweb.pojo.ExaminationAnswerSheetComExlVO;
import com.jinzhi.jzweb.pojo.ExaminationAnswerSheetComVO;
import com.jinzhi.jzweb.pojo.VtatisticsVo;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.jzweb.strategy.factory.SourConfig;
import com.jinzhi.jzweb.utils.DoubleUtil;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import com.jinzhi.sys.service.SysUserRelevancyService;
import com.jinzhi.sys.service.UserRoleService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 超级管理员 总裁篇查看分数
 * <pre>
 * B赛场查看分数 统计
 * 2023
 * </pre>
 * <small> 2023-05-05 15:11:09 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/examinationAnswerSheetComSour")
public class ExaminationAnswerSheetComSourController extends BaseController {
    @Autowired
    private ExaminationAnswerSheetComService examinationAnswerSheetComService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private ExaminationAnswerSheetComService examinationAnswerSheetService;
    @Autowired
    private ExaminationPaperService examinationPaperService;
    @Autowired
    private StuSignUpComService stuSignUpService;
    @Autowired
    private ExaminationQuestionsService examinationQuestionsService;
    @Autowired
    private ExaminationBatchService examinationBatchService;
    @Autowired
    private ExaminationResultsService resultsService;
    @Autowired
    private ExaminationResultsComService examinationResultsService;
    @Autowired
    private ApittController apittController;
    @Autowired
    private SysUserRelevancyService userRelevancyService;

    /**
     * 超级管理员 一级管理员查看分数
     *
     * @param model
     * @return
     */
    @GetMapping()
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:examinationAnswerSheetComSour")
    String info(Model model,String distinguish) {
        String cbatchId = userRelevancyService.selectById(this.getUser().getUserRelevancyId()).getTrack();
        model.addAttribute("pid",cbatchId);
        return "jzweb/examinationAnswerSheetCom/examinationAnswerSheetComSour";
    }

    @GetMapping(value = "/exitScore")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:exitScore")
    String exitScore(Model model) {
        return "jzweb/examinationAnswerSheetCom/exitScore";
    }

    /**
     * B赛场成绩导出
     *
     * @param response
     */
    @GetMapping("/downloadImportStu")
    @RequiresPermissions("jzweb:stuSignUpCom:import")
    @ResponseBody
    public void downloadImportTemplate(HttpServletResponse response, ExaminationResultsComDO examinationAnswerSheetComDT) {
        List<ExaminationAnswerSheetComExlVO> list1 = new LinkedList<>();
        Wrapper<StuSignUpComDO> wrapper = new EntityWrapper<>();

        if (StringUtils.isNotBlank(examinationAnswerSheetComDT.getUnitName())) {
            wrapper.like("unit_name", examinationAnswerSheetComDT.getUnitName());
        }
        if (StringUtils.isNotEmpty(examinationAnswerSheetComDT.getNum2())){
            wrapper.like("candidate_number", examinationAnswerSheetComDT.getNum2());
        }
        // 查询列表数据
        Page<StuSignUpComDO> list = stuSignUpService.selectPage(getPage(StuSignUpComDO.class),
                wrapper.eq("decrypt",1).
                        orderBy("id", false));
        for (StuSignUpComDO stuSignUpComDO : list.getRecords()) {
            ExaminationResultsComDO examinationResultsComDO = examinationResultsService.findOneByKv("stu_sign_id", stuSignUpComDO.getId());
            if (examinationResultsComDO != null) {
                ExaminationResultsComDO obj = examinationResultsComDO;
                ExaminationAnswerSheetComExlVO exlVO = new ExaminationAnswerSheetComExlVO();
                exlVO.setNum2(stuSignUpComDO.getCandidateNumber());
                exlVO.setName(stuSignUpComDO.getName());
                exlVO.setUnitName(stuSignUpComDO.getUnitName());
                exlVO.setIdNumber(stuSignUpComDO.getIdNumber());

                //  exlVO.setTicketNumber(stuSignUpComDO.getTicketNumber());
                if (obj.getScore1() == null) {
                    obj.setScore1(0.0);
                }
                if (obj.getScore2() == null) {
                    obj.setScore2(0.0);
                }
                if (obj.getScore3() == null) {
                    obj.setScore3(0.0);
                }
                if (obj.getScore4() == null) {
                    obj.setScore4(0.0);
                }
                if (obj.getScore5() == null) {
                    obj.setScore5(0.0);
                }
                if (obj.getScore1s() == null) {
                    obj.setScore1s(0.0);
                }
                if (obj.getScore2s() == null) {
                    obj.setScore2s(0.0);
                }
                if (obj.getScore3s() == null) {
                    obj.setScore3s(0.0);
                }
                if (obj.getScore4s() == null) {
                    obj.setScore4s(0.0);
                }
                if (obj.getScore5s() == null) {
                    obj.setScore5s(0.0);
                }

                double s11 = (obj.getScore1());
                double f11 = DoubleUtil.douToTwo(s11);

                double s11s = (obj.getScore1s());
                double f11s = DoubleUtil.douToTwo(s11s);
                String a = f11 + "";

                double s12 = (obj.getScore2());
                double f12 = DoubleUtil.douToTwo(s12);

                double s12s = (obj.getScore2s());
                double f12s = DoubleUtil.douToTwo(s12s);
                String b = f12 + "";

                double s13 = (obj.getScore3());
                double f13 = DoubleUtil.douToTwo(s13);

                double s13s = (obj.getScore3s());
                double f13s = DoubleUtil.douToTwo(s13s);
                String c = f13 + "";

                double s14 = (obj.getScore4());
                double f14 = DoubleUtil.douToTwo(s14);

                double s14s = (obj.getScore4s());
                double f14s = DoubleUtil.douToTwo(s14s);
                String d = f14 + "";

                double s15 = (obj.getScore5());
                double f15 = DoubleUtil.douToTwo(s15);

                double s15s = (obj.getScore5s());
                double f15s = DoubleUtil.douToTwo(s15s);
                String e = f15 + "";

                double s1= Double.parseDouble((a));
                double s2= Double.parseDouble((b));
                double s3= Double.parseDouble((c));
                double jg = s1 + s2 +s3;
                jg = (jg / 3);
                BigDecimal bg = new BigDecimal(jg);

                exlVO.setScore1(a);
                exlVO.setScore2(b);
                exlVO.setScore3(c);
                double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                exlVO.setRemark(examinationResultsComDO.getRemarkB());
                exlVO.setScore(String.valueOf(f1));
                list1.add(exlVO);
            } else {
                //  ExaminationResultsComDO obj = new ExaminationResultsComDO();
                ExaminationAnswerSheetComExlVO obj = new ExaminationAnswerSheetComExlVO();
                obj.setNum2(stuSignUpComDO.getCandidateNumber());
                obj.setName(stuSignUpComDO.getName());
                obj.setUnitName(stuSignUpComDO.getUnitName());
                obj.setIdNumber(stuSignUpComDO.getIdNumber());
                obj.setScore1("0.0");
                obj.setScore2("0.0");
                obj.setScore3("0.0");
                obj.setScore("0.0");
                list1.add(obj);

            }
            // list1.add(stuSignUpComVo);
            list1 = list1.stream().sorted(Comparator.comparing(ExaminationAnswerSheetComExlVO::getNum2,Comparator.nullsLast(String::compareTo))).collect(Collectors.toList());
        }

        //导出操作
        EasyPoiUtil.exportExcel(list1, examinationAnswerSheetComDT.getNum2() + "赛场成绩导出", examinationAnswerSheetComDT.getNum2() +"赛场成绩导出", ExaminationAnswerSheetComExlVO.class, examinationAnswerSheetComDT.getNum2() +"赛场成绩导出", response);
    }

    /**
     * B赛场理论成绩统计
     *
     * @param examinationAnswerSheetComDTO
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:examinationAnswerSheetComSour")
    public Result<VtatisticsVo> list(ExaminationResultsComDO examinationAnswerSheetComDTO) {
        VtatisticsVo vtatisticsVo = new VtatisticsVo();
        Wrapper<StuSignUpComDO> wrapper = new EntityWrapper<>();

        if (StringUtils.isNotBlank(examinationAnswerSheetComDTO.getUnitName())) {
            wrapper.like("unit_name", examinationAnswerSheetComDTO.getUnitName());
        }
        if (StringUtils.isNotEmpty(examinationAnswerSheetComDTO.getNum2())){
            wrapper.like("candidate_number", examinationAnswerSheetComDTO.getNum2());
        }

        // 查询列表数据
        Page<StuSignUpComDO> page = stuSignUpService.selectPage(getPage(StuSignUpComDO.class),
                wrapper.orderBy("id", false));
        double f1 = 0.0; //个人总分
        double max = 0.0; //最高分
        double min = 100.0; //最低分
        List<ExaminationResultsComDO> list = new ArrayList();
        for (int i = 0; i < page.getRecords().size(); i++) {
            ExaminationResultsComDO examinationResultsComDO = examinationResultsService.findOneByKv("stu_sign_id", page.getRecords().get(i).getId());
            StuSignUpComDO stuSignUpComDO = page.getRecords().get(i);
            if (examinationResultsComDO != null) {
                ExaminationResultsComDO obj = examinationResultsComDO;
                obj.setNum2(stuSignUpComDO.getCandidateNumber());
                obj.setName(stuSignUpComDO.getName());
                obj.setUnitName(stuSignUpComDO.getUnitName());
                obj.setIdNumber(stuSignUpComDO.getIdNumber());
                obj.setTicketNumber(stuSignUpComDO.getTicketNumber());
                obj.setDecrypt(stuSignUpComDO.getDecrypt());
                Double sour = obj.getTotalScore();
                if (obj.getScore1() == null) {
                    obj.setScore1(0.0);
                }
                if (obj.getScore2() == null) {
                    obj.setScore2(0.0);
                }
                if (obj.getScore3() == null) {
                    obj.setScore3(0.0);
                }
                if (obj.getScore4() == null) {
                    obj.setScore4(0.0);
                }
                if (obj.getScore5() == null) {
                    obj.setScore5(0.0);
                }
                if (obj.getScore1s() == null) {
                    obj.setScore1s(0.0);
                }
                if (obj.getScore2s() == null) {
                    obj.setScore2s(0.0);
                }
                if (obj.getScore3s() == null) {
                    obj.setScore3s(0.0);
                }
                if (obj.getScore4s() == null) {
                    obj.setScore4s(0.0);
                }
                if (obj.getScore5s() == null) {
                    obj.setScore5s(0.0);
                }

                double s11 = (obj.getScore1());
                double f11 = DoubleUtil.douToTwo(s11);

                double s11s = (obj.getScore1s());
                double f11s = DoubleUtil.douToTwo(s11s);
                String a = f11 + "";

                double s12 = (obj.getScore2());
                double f12 = DoubleUtil.douToTwo(s12);

                double s12s = (obj.getScore2s());
                double f12s = DoubleUtil.douToTwo(s12s);
                String b = f12 + "";

                double s13 = (obj.getScore3());
                double f13 = DoubleUtil.douToTwo(s13);

                double s13s = (obj.getScore3s());
                double f13s = DoubleUtil.douToTwo(s13s);
                String c = f13 + "";

                double s14 = (obj.getScore4());
                double f14 = DoubleUtil.douToTwo(s14);

                double s14s = (obj.getScore4s());
                double f14s = DoubleUtil.douToTwo(s14s);
                String d = f14 + "";

                double s15 = (obj.getScore5());
                double f15 = DoubleUtil.douToTwo(s15);

                double s15s = (obj.getScore5s());
                double f15s = DoubleUtil.douToTwo(s15s);
                String e = f15 + "";

                obj.setScoreA(a);
                obj.setScoreB(b);
                obj.setScoreC(c);
                double s1= Double.parseDouble((a));
                double s2= Double.parseDouble((b));
                double s3= Double.parseDouble((c));
                double jg = s1 + s2 +s3;
                jg = (jg / 3);
                BigDecimal bg = new BigDecimal(jg);
                f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

                obj.setScore(f1); //赛场得分
                obj.setRemarkB(examinationResultsComDO.getRemarkB());
                list.add(obj);
            } else {
                ExaminationResultsComDO obj = new ExaminationResultsComDO();
                obj.setNum2(stuSignUpComDO.getCandidateNumber());
                obj.setName(stuSignUpComDO.getName());
                obj.setUnitName(stuSignUpComDO.getUnitName());
                obj.setIdNumber(stuSignUpComDO.getIdNumber());
                obj.setTicketNumber(stuSignUpComDO.getTicketNumber());
                obj.setDecrypt(stuSignUpComDO.getDecrypt());
                Double sour = obj.getTotalScore();
                obj.setScore1(0.0);
                obj.setScore2(0.0);
                obj.setScore3(0.0);
                obj.setScore4(0.0);
                obj.setScore5(0.0);
                obj.setScore1s(0.0);
                obj.setScore2s(0.0);
                obj.setScore3s(0.0);
                obj.setScore4s(0.0);
                obj.setScore5s(0.0);
                obj.setScoreA("0.0");
                obj.setScoreB("0.0");
                obj.setScoreC("0.0");
                obj.setScore(0.0);
                list.add(obj);

            }
            //获取最高分
            if (f1 >= max) {
                max = f1;
                vtatisticsVo.setMaxScore(max);
            }
            //获取最低分
            if (f1 <= min) {
                min = f1;
                vtatisticsVo.setMinScore(min);
            }
        }
        List list1 = list.stream().sorted(Comparator.comparing(ExaminationResultsComDO::getNum2,Comparator.nullsLast(String::compareTo))).collect(Collectors.toList());
        page.setRecords(list1);
        //获取竞赛人数
        vtatisticsVo.setNumberPeople(page.getRecords().size());
        vtatisticsVo.setStuSignUpComDOs(page);
        return Result.ok(vtatisticsVo);
    }


    @GetMapping("/edit/{id}")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:edit")
    String edit(@PathVariable("id") Long id, Model model) {


        ExaminationAnswerSheetComDO examinationAnswerSheetCom = examinationAnswerSheetComService.selectById(id);
        ExaminationAnswerSheetComVO answerSheetComVO = new ExaminationAnswerSheetComVO();
        String vauel = examinationAnswerSheetCom.getAnswerList();
        JSONArray jsonArray = JSONObject.parseArray(vauel);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObj = jsonArray.getJSONObject(i);
            String type = jsonObj.getString("type");
            if (type.equals("4")) {
                StuSignUpComDO stuSignUpComDO = stuSignUpService.selectById(examinationAnswerSheetCom.getStuSignId());
                if (stuSignUpComDO != null) {
                    answerSheetComVO.setAnswerList(jsonObj.getString("value"));
                    answerSheetComVO.setState(stuSignUpComDO.getState().toString());
                    answerSheetComVO.setNum2(stuSignUpComDO.getNum2());
                    answerSheetComVO.setCourt(stuSignUpComDO.getCourt());
                    answerSheetComVO.setId(stuSignUpComDO.getId());
                    answerSheetComVO.setExamBatchId(stuSignUpComDO.getCbatchId());
                }
                model.addAttribute("answerSheetComVO", answerSheetComVO);
            }
        }
        return "jzweb/examinationAnswerSheetCom/edit";
    }

    @Log("添加考试答卷")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:add")
    public Result<String> save(ExaminationAnswerSheetComDO examinationAnswerSheetCom) {
        examinationAnswerSheetComService.insert(examinationAnswerSheetCom);
        return Result.ok();
    }

    @Log("修改考试答卷")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:edit")
    public Result<String> update(ExaminationAnswerSheetComDO examinationAnswerSheetCom) {
        boolean update = examinationAnswerSheetComService.updateById(examinationAnswerSheetCom);
        return update ? Result.ok() : Result.fail();
    }

    @Log("删除考试答卷")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:remove")
    public Result<String> remove(Long id) {
        examinationAnswerSheetComService.deleteById(id);
        return Result.ok();
    }

    @Log("批量删除考试答卷")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:batchRemove")
    public Result<String> remove(@RequestParam("ids[]") Long[] ids) {
        examinationAnswerSheetComService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }

}
